<?php include_once '../../essential.php'; ?>

<table>
	<tr class="title">
		<td style='width: 4%'><a href='#' onclick="checkAll();">All</a></td>
		<td style='width: 15%'>Data Cadastro</td>
		<td style='width: 50%'>Nome</td>
		<td style='width: 13%'>Grupo</td>
		<td style="width: 7%;">Status</td>
	</tr>
	<?php
		$user = new User();
		
		$condition = "";
		
		if(isset($_GET['filter']))
		{
			$condition = $_GET['filter'];
		}
					
		$result =  $user->listAll("sys_user.*, sys_group.name group_name", "LEFT JOIN sys_group ON sys_user.id_group = sys_group.id ".$condition. " ORDER BY name ASC ");
					
		while($usr = mysql_fetch_object($result))
		{
			echo "<tr>";
			echo "<td style='text-align:center;'><input type='checkbox' value='".$usr->id."' class='check-to'/></td>";
			echo "<td>".$usr->dt_register."</td>";
			echo "<td>".$usr->name."</td>";
			echo "<td>".$usr->group_name."</td>";
			echo "<td style='text-align:center;'><img style='width: 30px;' src='images/".($usr->active ? 'active' : 'inactive').".png'/></td>";
			echo "</tr>";
		}
	?>
</table>

<script>
	function checkAll()
	{
		if($(".check-to").attr("checked"))
		{
			$(".check-to").removeAttr("checked");
			$(".check-to").parents("tr").removeClass("activeCheck");
		}else{
			$(".check-to").attr("checked", "true");
			$(".check-to").parents("tr").addClass("activeCheck");
		}
	}

	function editItem()
	{
		if($("input:checked").length > 1 )
		{
			alert("È permitido apenas a edição de um item!. desmarque os outros itens!");
		}else if($("input:checked").length == 0){
			alert("Selecione um item para edição, marcando a caixa do lado esquerdo.");
		}else{
			openForm('user','form', $("input:checked").val());
		}
	}

	$(document).ready(function(){
		$(".check-to").bind("mousedown", function(){
			$(this).parents("tr").toggleClass("activeCheck");
		});
	});	
</script>